{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.先导入需要的模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们api_secret、api_key\n",
    "api_secret = \"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\"\n",
    "api_key = 'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov'\n",
    "\n",
    "# 3.目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://www.apple.com.cn/newsroom/images/live-action/keynote-september-2020/apple_apple-event-keynote_tim_09152020_big.jpg.large_2x.jpg'\n",
    "\n",
    "# 4.沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5.准备后面的数据\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    # 是否检测并返回根据人脸特征判断出的年龄、性别、情绪等属性。\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "# 6.requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603021269,729c11ef-2417-4da5-b9a6-14fef225a010\",\"time_used\":1194,\"faces\":[{\"face_token\":\"1aff0090208b37e2f8de321a8e2e96c7\",\"face_rectangle\":{\"top\":139,\"left\":583,\"width\":140,\"height\":140},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":66},\"smile\":{\"value\":28.454,\"threshold\":50.000},\"emotion\":{\"anger\":0.040,\"disgust\":37.393,\"fear\":0.040,\"happiness\":24.038,\"neutral\":37.699,\"sadness\":0.735,\"surprise\":0.054}}}],\"image_id\":\"AfGvmVi3qesqsPIfULDZkw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603021269,729c11ef-2417-4da5-b9a6-14fef225a010',\n",
       " 'time_used': 1194,\n",
       " 'faces': [{'face_token': '1aff0090208b37e2f8de321a8e2e96c7',\n",
       "   'face_rectangle': {'top': 139, 'left': 583, 'width': 140, 'height': 140},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 66},\n",
       "    'smile': {'value': 28.454, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.04,\n",
       "     'disgust': 37.393,\n",
       "     'fear': 0.04,\n",
       "     'happiness': 24.038,\n",
       "     'neutral': 37.699,\n",
       "     'sadness': 0.735,\n",
       "     'surprise': 0.054}}}],\n",
       " 'image_id': 'AfGvmVi3qesqsPIfULDZkw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# requests 巧妙的方法   r = response\n",
    "results = r.json() \n",
    "results"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
